package com.duobaoyu.dby.supply.biz.client.resource.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.common.core.constant.SecurityConstants;
import com.duobaoyu.dby.common.core.constant.ServiceNameConstants;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.resource.admin.feign.RemoteResourceChannelConfigAdminService;
import com.duobaoyu.dby.resource.common.admin.dto.config.ChannelConfigDTO;
import com.duobaoyu.dby.resource.remote.common.to.config.ChannelConfigTO;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceChannelConfigAdminClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.ChannelConfigBizDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.ChannelConfigVO;
import com.duobaoyu.dby.supply.biz.client.resource.converter.ResourceConverter;
import com.duobaoyu.dby.supply.biz.util.RemoteResultCheckUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName ResourceChannelConfigAdminClientImpl
 * @createTime 13:18:00 2023/10/08
 */
@Component
@RequiredArgsConstructor
public class ResourceChannelConfigAdminClientImpl implements ResourceChannelConfigAdminClient {

    private final ResourceConverter resourceConverter;
    private final RemoteResourceChannelConfigAdminService resourceChannelConfigAdminService;
    @Override
    public boolean updateChannelConfig(ChannelConfigBizDTO channelConfig) {
        ChannelConfigDTO dto = resourceConverter.toChannelConfig(channelConfig);
        R<Boolean> result = resourceChannelConfigAdminService.update(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, channelConfig);
        return result.getData();
    }

    @Override
    public boolean removeChannelConfig(Long channelConfigId) {
        R<Boolean> result = resourceChannelConfigAdminService.delete(channelConfigId, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, channelConfigId);
        return result.getData();
    }

    @Override
    public boolean addChannelConfig(ChannelConfigBizDTO channelConfig) {
        ChannelConfigDTO dto = resourceConverter.toChannelConfig(channelConfig);
        R<Boolean> result = resourceChannelConfigAdminService.add(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, channelConfig);
        return result.getData();
    }

    @Override
    public ChannelConfigVO getById(Long channelConfigId) {
        R<ChannelConfigTO> result = resourceChannelConfigAdminService.queryOne(channelConfigId, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, channelConfigId);
        return resourceConverter.toChannelConfigView(result.getData());
    }

    @Override
    public Page<ChannelConfigVO> page(ChannelConfigBizDTO channelConfig) {
        ChannelConfigDTO dto = resourceConverter.toChannelConfig(channelConfig);
        R<Page<ChannelConfigTO>> result = resourceChannelConfigAdminService.page(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, channelConfig);
        return resourceConverter.toChannelConfigPage(result.getData());
    }
}
